import Api from "@/api/api-declare";
import PD from '@/pd-libs/libs';
import Store from '@/store';
import type { ApiImTypes } from "@/api/api-types/api-im-types";
// import { WK_MSG_TYPES,READ_STATE } from "@/pd-libs/class/wkim/wkim";
import { wkim } from '@/pd-libs/class/wkim/wkim';

export function useImMsg(){
	const imUid = ref('');
	const imToken = ref('');
	//消息列表参数
	const imListQuery = ref<ApiImTypes.GetMsgHistoryReq>({
		channel_id : '',
		channel_type : 2,
		end_message_seq : 0,
		start_message_seq : 0,
		limit : 9999
	});
	//消息列表
	const imLists = ref<ApiImTypes.ImListItemType[]>([]);
	//清除某个会话的未读消息数量 - 参数
	const imClearUnreadQuery = ref<ApiImTypes.ClearUnreadReq>({
		channel_type : 2,
		unread : 0,
		// unread : READ_STATE.unread,
		channel_id : ''
	});
	//消息设置已读 - 参数
	const imMsgsReadQuery = ref<ApiImTypes.MsgsReadReq>({
		message_ids : [],
		channel_id : '',
		channel_type : 2
	});
	//im登陆
	const imLogin = () : Promise<ApiImTypes.ImLoginRes> => {
		return new Promise((r) => {
			const loginData = {
				username : `0086${Store.driver().info.mobile}`,
				password : Store.driver().info.mobile,
				flag:1
			};
			Api.ApiIm.login(loginData).then(res => {
				imToken.value = res.token;
				imUid.value = res.uid;
				r(res);
			});
		});
	};
	//同步消息 - 获取消息列表
	const imListGet = () : Promise<void> => {
		return new Promise((r) => {
			Api.ApiIm.getMsgHistory(imListQuery.value).then(res => {
				if(res.messages && res.messages.length){
					// 目前仅支持文本消息
					const messages : ApiImTypes.MessagesItemType[] = res.messages.filter(v => v.payload.type === 1);
					const result : ApiImTypes.ImListItemType[] = messages.map(v => {
						return {
							from_uid : v.from_uid,
							client_msg_no : v.client_msg_no,
							message_idstr : v.message_idstr,
							message_id : v.message_id,
							is_my_msg : v.from_uid === PD.UniHelp.getStorage('imUid'),
							readed : Boolean(v.readed),
							timestamp : v.timestamp,
							payload : {
								content : v.payload.type === 1 ? v.payload.content : '未知消息',
								// content : v.payload.type === WK_MSG_TYPES.text ? v.payload.content : '未知消息',
								type : v.payload.type
							}
						};
					});
					imLists.value = result;
				};
				r();
			});
		});
	};
	//清除某个会话的未读消息数量
	const imClearUnread = () : Promise<void> => {
		return new Promise((r) => {
			Api.ApiIm.clearUnread(imClearUnreadQuery.value).then(() => {
				r();
			});
		});
	};
	//消息设置已读
	const imMsgsRead = () : Promise<void> => {
		return new Promise((r) => {
			Api.ApiIm.msgsRead(imMsgsReadQuery.value).then(() => {
				r();
			});
		});
	};
	//列表设置已读
	const imSetRead = () => {
		imLists.value.forEach(item => {
			item.readed = true;
		});
	};
	//往im列表中添加一条数据
	const imListsAdd = (e : ApiImTypes.ImListItemType) => {
		imLists.value.push(e);
	};
	//获取im配置信息
	const imConfig = () : Promise<void> => {
		return new Promise((r) => {
			Api.ApiIm.getImConfig({}).then(res => {
				//wkim初始化
				wkim.init({
					token : imToken.value,
					url : res.wss_addr,
					uid : imUid.value
				});
				r();
			});
		});
	};
	//请求哈啰订单未读数量
	const getHaLuoUnreadList = () : Promise<ApiImTypes.ConversationSyncRes> => {
		return new Promise((r) => {
			Api.ApiIm.conversationSync().then(res => {
				r(res);
			});
		});
	};
	return {
		imLogin,
		imListQuery,
		imListGet,
		imLists,
		imClearUnreadQuery,
		imClearUnread,
		imMsgsRead,
		imMsgsReadQuery,
		imUid,
		imSetRead,
		imListsAdd,
		imConfig,
		getHaLuoUnreadList
	};
};